package main

import (
	"InstantCommunication/modules"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
)

func main() {
	db, err := gorm.Open(mysql.Open("root:Dzy001016@tcp(127.0.0.1:3306)/ginchat?charset=utf8mb4&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	// Migrate the schema
	db.AutoMigrate(&modules.UserBasic{})

	// Create
	user := &modules.UserBasic{}
	user.Name = "XXX"
	loginTime := time.Now()
	time.Sleep(time.Second * 2)
	logoutTime := time.Now()
	user.LoginTime = loginTime
	user.LogoutTime = logoutTime
	user.HeartbeatTime = loginTime.Add(logoutTime.Sub(loginTime))
	db.Create(user)

	// Read
	fmt.Println(db.First(user, 1)) // find product with integer primary key
	//db.First(&product, "code = ?", "D42") // find product with code D42

	//db.Find(user, 1).Scan(user)
	//fmt.Println(user)

	// Update - update product's price to 200
	db.Model(user).Update("PassWord", 123)
	// Update - update multiple fields
	//db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields
	//db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

	// Delete - delete product
	//db.Delete(&product, 1)
}
